package org.hgm.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.springframework.social.twitter.api.Tweet;
import org.springframework.social.twitter.api.Twitter;
import org.springframework.social.twitter.api.TwitterProfile;
import org.springframework.social.twitter.api.impl.TwitterTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
public class TwitterController {
	@RequestMapping(value = "/tw.htm", method = RequestMethod.GET)
	public String test3(HttpServletResponse response) throws IOException {
		Twitter twitter = new TwitterTemplate("QkjRB3ptAfSNByPcuQhZHw", "jNe1If9nVDQQoRGUz7eJNXY8U1HPaw4Cr1CzjdiHxs",
				"142276948-lOGYE7ixRCqkqvBPANsxTwNO5jCYKqomjlkdqfLV", "1rKb0VVYg8GCWiflP2HEkQDBkOFqHR6we3KUOkiaRI");
		TwitterProfile profile = twitter.userOperations().getUserProfile();
		List<Tweet> tweets = twitter.timelineOperations().getHomeTimeline();
		List<TwitterDTO> resultMap = getResultMap(tweets);
		tweets = twitter.timelineOperations().getUserTimeline("mansunil");
		return "result";
	}

	private List<TwitterDTO> getResultMap(List<Tweet> tweets) {
		List<TwitterDTO> twitterDTOs = new ArrayList<TwitterDTO>();
		for (Tweet tweet : tweets) {
			TwitterDTO twitterDTO = new TwitterDTO();
			twitterDTO.setProfileImage(tweet.getProfileImageUrl());
			String text = tweet.getText();
			System.out.println(text);
			// twitterDTO.setText(text.substring(text.lastIndexOf("http://")));
			// twitterDTO.setDetailsLinkAboutText(text.substring(text.lastIndexOf("http://"), text.length()));
			twitterDTOs.add(twitterDTO);
		}
		return twitterDTOs;
	}
}
